package com.ultrasoft.farmasoft.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Accountreceivable;
import com.ultrasoft.farmasoft.domain.Accountspayable;
import com.ultrasoft.farmasoft.domain.Bill;
import com.ultrasoft.farmasoft.domain.Client;
import com.ultrasoft.farmasoft.domain.Credititem;
import com.ultrasoft.farmasoft.domain.Dosage;
import com.ultrasoft.farmasoft.domain.Item;
import com.ultrasoft.farmasoft.domain.Locality;
import com.ultrasoft.farmasoft.domain.Operation;
import com.ultrasoft.farmasoft.domain.Package;
import com.ultrasoft.farmasoft.domain.Paymentplan;
import com.ultrasoft.farmasoft.domain.Pharmacy;
import com.ultrasoft.farmasoft.domain.Pharmacyproduct;
import com.ultrasoft.farmasoft.domain.Product;
import com.ultrasoft.farmasoft.domain.Region;
import com.ultrasoft.farmasoft.domain.Transaction;
import com.ultrasoft.farmasoft.domain.User;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
import com.ultrasoft.farmasoft.exceptions.CustomException;
import com.ultrasoft.farmasoft.repository.AccountReceivableDao;
import com.ultrasoft.farmasoft.repository.AccountsPayableDao;
import com.ultrasoft.farmasoft.repository.BillDao;
import com.ultrasoft.farmasoft.repository.ClientDao;
import com.ultrasoft.farmasoft.repository.CreditItemDao;
import com.ultrasoft.farmasoft.repository.DosageDao;
import com.ultrasoft.farmasoft.repository.ItemDao;
import com.ultrasoft.farmasoft.repository.LocalityDao;
import com.ultrasoft.farmasoft.repository.OperationDao;
import com.ultrasoft.farmasoft.repository.PackageDao;
import com.ultrasoft.farmasoft.repository.PaymentPlanDao;
import com.ultrasoft.farmasoft.repository.PharmacyDao;
import com.ultrasoft.farmasoft.repository.PharmacyProductDao;
import com.ultrasoft.farmasoft.repository.RegionDao;
import com.ultrasoft.farmasoft.repository.TransactionDao;
import com.ultrasoft.farmasoft.repository.UserDao;
import com.ultrasoft.farmasoft.repository.UserPharmacyDao;
import com.ultrasoft.farmasoft.util.UtilCommon;
import com.ultrasoft.farmasoft.util.UtilConverter;


@Service
public class BuyProductsTransactionServiceImpl implements	BuyProductsTransactionService {

	private PackageDao packageDao;
	private PharmacyProductDao pharmacyProductDao;
	private PharmacyDao pharmacyDao;
	private DosageDao dosageDao;
	private ItemDao itemDao;
	private TransactionDao transactionDao;
	private OperationDao operationDao;
	private ClientDao clientDao;
	private BillDao billDao;
	private UserPharmacyDao userPharmacyDao;
	private LocalityDao localityDao;
	private RegionDao regionDao;
	private UserDao userDao;
	
	private AccountsPayableDao accountsPayableDao;
	private AccountReceivableDao accountReceivableDao;
	private PaymentPlanDao paymentPlanDao;
	
	private CreditItemDao creditItemDao;
	
	@Autowired
	public BuyProductsTransactionServiceImpl(CreditItemDao creditItemDao,PaymentPlanDao paymentPlanDao,AccountReceivableDao accountReceivableDao,AccountsPayableDao accountsPayableDao,UserDao userDao,RegionDao regionDao,LocalityDao localityDao,UserPharmacyDao userPharmacyDao,BillDao billDao,DosageDao dosageDao,PharmacyDao pharmacyDao,ClientDao clientDao,OperationDao operationDao,TransactionDao transactionDao,ItemDao itemDao,PharmacyProductDao pharmacyProductDao,PackageDao packageDao){
		this.packageDao 			= packageDao;
		this.pharmacyProductDao 	= pharmacyProductDao;
		this.itemDao 				= itemDao;
		this.transactionDao  		= transactionDao;
		this.operationDao 			= operationDao;
		this.clientDao 				= clientDao;
		this.pharmacyDao 			= pharmacyDao;
		this.dosageDao 				= dosageDao;
		this.billDao 				= billDao;
		this.userPharmacyDao 		= userPharmacyDao;
		this.localityDao 			= localityDao;
		this.regionDao 				= regionDao;
		this.userDao 				= userDao;
		this.accountsPayableDao 	= accountsPayableDao;
		this.accountReceivableDao 	= accountReceivableDao;
		this.paymentPlanDao 		= paymentPlanDao;		
		this.creditItemDao 			= creditItemDao;
	}
	@Transactional(readOnly = true)
	public List<Package> getListPackage(){
		return packageDao.getListPackage();
	}
	@Transactional(readOnly = true)
	public List<Package> getListPackageByActive(boolean active){
		return packageDao.getListPackageByActive(active);
	}
	@Transactional(readOnly = true)
	public List<Package> getListPackageByPharmacyProductByActive(Pharmacyproduct pharmacyproduct,boolean active){
		return packageDao.getListPackageByPharmacyProductByActive(pharmacyproduct, active);
	}
	@Transactional(readOnly = true)
    public Package searchPackageByPackageId(Integer packageId){
    	return packageDao.findPackageByPackageId(packageId);
    }    
	@Transactional
    public void updatePackage(Package packageElement){
    	packageDao.update(packageElement);
    }
	@Transactional(rollbackFor = {Exception.class})
	public void updatePackageByListPackage(List<Package> listPackage) {
		for (Package package1 : listPackage) {
			Package p = packageDao.findPackageByPackageId(package1.getPackageId());
			p.setState(package1.getState());
			packageDao.update(p);			
		}
	}
	@Transactional
    public void removePackage(Package packageElement){
    	packageDao.remove(packageElement);
    }    
	@Transactional
    public void addPackage(Package packageElement){
    	packageDao.add(packageElement);
    }
	
	
	
	@Transactional(readOnly = true)
	public Pharmacyproduct searchPharmacyProductByPharmacyIdByProviderIdByProductId(Integer pharmacyId,Integer providerId, Integer productId){
		return pharmacyProductDao.findPharmacyProductByPharmacyIdByProviderIdByProductId(pharmacyId, providerId, productId);
	}
	@Transactional
	public void addPharmacyProduct(Pharmacyproduct pharmacyProduct) {
		pharmacyProductDao.add(pharmacyProduct);
	}
	@Transactional
	public void updatePharmacyProduct(Pharmacyproduct pharmacyProduct) {
		pharmacyProductDao.update(pharmacyProduct);
	}
	
	
	
	@Transactional(readOnly = true)
	public List<Item> getListItemByPackageId(Integer packageId){
		return itemDao.getListItemByPackageId(packageId);
	}
	@Transactional(readOnly = true)
	public Integer searchByItemIdMax(Integer packageId){
		return itemDao.findByItemIdMax(packageId);
	}
	@Transactional(readOnly = true)
	public Item searchItemByPackageIdByItemId(Integer packageId, Integer itemId){
		return itemDao.findItemByPackageIdByItemId(packageId, itemId);
	}
	@Transactional
	public void updateItem(Item item) {
		itemDao.update(item);
	}
	@Transactional(rollbackFor = {Exception.class})
	public void updateItemByListItem(List<Item> items) {
		for (Item item : items) {
			Item i = itemDao.findItemByPackageIdByItemId(item.getItemPK().getPackageId(),item.getItemPK().getItemId());
			i.setState(item.getState());
			i.setTempState(item.getTempState());
			itemDao.update(i);			
		}
	}
	@Transactional
	public void addItem(Item item) {
		itemDao.add(item);
	}
	
	
	
	@Transactional(readOnly = true)
	public List<Transaction> getListTransaction(){
		return transactionDao.getListTransaction();
	}
	@Transactional(readOnly = true)
	public List<Transaction> getListTransactionByListPharmacyByStartDateByEndDate(List<Pharmacy> listPharmacy,Date start,Date end){
		List<Transaction> listTransaction = new ArrayList<Transaction>();
		for (Pharmacy pharmacy : listPharmacy) {
			listTransaction.addAll(getListTransactionByStartDateByEndDate(pharmacy.getPharmacyId(),start,end));
		}
		return listTransaction;
	}
	@Transactional(readOnly = true)
	public List<Transaction> getListTransactionByPharmacyIdByStartDateByEndDate(Integer pharmacyId,Date start,Date end){
		List<Transaction> listTransaction = new ArrayList<Transaction>();
		listTransaction.addAll(getListTransactionByStartDateByEndDate(pharmacyId,start,end));
		return listTransaction;
	}
	private List<Transaction> getListTransactionByStartDateByEndDate(Integer pharmacyId,Date start,Date end){
		List<Transaction> listTransaction = transactionDao.getListTransactionByPharmacyIdByStartDateByEndDate(pharmacyId,start, end);
		for (Transaction transaction : listTransaction) {
			if(transaction.getBill() != null){
				Bill bill = billDao.findBillByPeriodIdByBillId(transaction.getBill().getBillPK().getPeriodId(),transaction.getBill().getBillPK().getBillId());
				if(bill.getDosageId() != null){
					Dosage dosage = dosageDao.findDosageByDosageId(bill.getDosageId().getDosageId());
					bill.setDosageId(dosage);
				}
				transaction.setBill(bill);
			}
			if(transaction.getUserpharmacy() != null){
				Userpharmacy up = userPharmacyDao.findByPharmacyIdByUserId(transaction.getUserpharmacy().getUserpharmacyPK().getPharmacyId(),transaction.getUserpharmacy().getUserpharmacyPK().getUserId());
				Pharmacy p = pharmacyDao.findByPharmacyId(transaction.getUserpharmacy().getUserpharmacyPK().getPharmacyId());
				if(p != null){
					Locality locality = localityDao.findLocalityByRegionIdByLocalityId(p.getLocality().getLocalityPK().getRegionId(),p.getLocality().getLocalityPK().getLocalityId());
					if(locality != null){
						Region region = regionDao.findRegionByRegionId(locality.getLocalityPK().getRegionId());
						if(region != null)
							locality.setRegion(region);
						p.setLocality(locality);
					}						
					up.setPharmacy(p);
				}
				User u = userDao.findByUserId(transaction.getUserpharmacy().getUserpharmacyPK().getUserId());
				if(u != null)
					up.setUser(u);
				transaction.setUserpharmacy(up);
			}				
			if(transaction.getOperationId() != null)
				transaction.setOperationId(operationDao.findByOperationId(transaction.getOperationId().getOperationId()));
			if(transaction.getClientId() != null){
				Client c = clientDao.findClientByClientId(transaction.getClientId().getClientId());
				Pharmacy pharmacy = pharmacyDao.findByPharmacyId(c.getPharmacyId().getPharmacyId());
				c.setPharmacyId(pharmacy);
				transaction.setClientId(c);
			}
		}		
		return listTransaction;
	}
	@Transactional(readOnly = true)
	public List<Transaction> getListTransactionByPackageId(Integer packageId){
		return transactionDao.getListTransactionByPackageId(packageId);
	}
	@Transactional(readOnly = true)
	public List<Transaction> getListTransactionByClientId(Integer clientId){
		return transactionDao.getListTransactionByClientId(clientId);
	}
	@Transactional(readOnly = true)
	public List<Transaction> getListTransactionByOperationId(Integer operationId){
		return transactionDao.getListTransactionByOperationId(operationId);
	}
	@Transactional(readOnly = true)
	public List<Transaction> getListTransactionByPeriodIds(Integer periodIds){
		return transactionDao.getListTransactionByPeriodIds(periodIds);
	}
	@Transactional(readOnly = true)
	public List<Transaction> getListTransactionByTransactionId(Integer transactionId){
		return transactionDao.getListTransactionByTransactionId(transactionId);
	}
	@Transactional(readOnly = true)
	public Transaction searchTransactionByPeriodIdsByTransactionId(Integer periodIds,Integer transactionId){
		return transactionDao.findTransactionByPeriodIdsByTransactionId(periodIds, transactionId);
	}
	@Transactional(readOnly = true)
	public Integer searchTransactionIdByPeriodIdsMax(Integer periodIds){
		return transactionDao.findTransactionIdByPeriodIdsMax(periodIds);
	}
	@Transactional
    public void removeTransaction(Transaction transaction){
		transactionDao.remove(transaction);
	}
	@Transactional
    public void updateTransaction(Transaction transaction){
		transactionDao.update(transaction);
	}
	@Transactional
	public void addTransaction(Transaction transaction){
		transactionDao.add(transaction);
	}
	
	
	@Transactional(readOnly = true)
    public Operation searchByOperationId(Short operationId){
		return operationDao.findByOperationId(operationId);
	}
	
	
	@Transactional(readOnly = true)
	public List<Client> getListClientByPharmacyIdByActive(Integer pharmacyId,boolean active){
		return clientDao.getListClientByPharmacyIdByActive(pharmacyId, active);
	}
	@Transactional(readOnly = true)
	public List<Client> getListClientByPharmacyIdByNitByActive(Integer pharmacyId,String nit,boolean active){
		return clientDao.getListClientByPharmacyIdByNitByActive(pharmacyId, nit, active);
	}
	@Transactional(readOnly = true)
	public List<Client> getListClientByPharmacyIdByNameByActive(Integer pharmacyId,String name,boolean active){
		return clientDao.getListClientByPharmacyIdByNameByActive(pharmacyId, name, active);
	}
	@Transactional
    public void addClient(Client client){
		clientDao.add(client);
	}
	

	@Transactional(rollbackFor = {Exception.class})
	public Client searchClient(HttpServletRequest request,Userpharmacy userPharmacy){		
		Client client = null;
		List<Client> listclient = clientDao.getListClientByNitByActive(UtilConverter.converterStringToString(request,"_nitCi"),true);
		if(!listclient.isEmpty()){
			client = listclient.get(0);
			if(client != null){
				/*
				List<Accountreceivable> listAccountReceivableTemp = new ArrayList<Accountreceivable>();
		        List<Accountreceivable> listAccountReceivable = accountReceivableDao.getListAccountReceivableByClientIdByPharmacyIdByUserId(client.getClientId(),userPharmacy.getUserpharmacyPK().getPharmacyId(),userPharmacy.getUserpharmacyPK().getUserId());
		        for (Accountreceivable accountreceivable : listAccountReceivable) {
					List<Paymentplan> listPaymentPlanTemp = new ArrayList<Paymentplan>();
					List<Paymentplan> listPaymentPlan = paymentPlanDao.getListPaymentPlanByAccountReceivableId(accountreceivable.getAccountReceivableId());
					for (Paymentplan paymentplan : listPaymentPlan) {
						listPaymentPlanTemp.add(paymentplan);
					}
					accountreceivable.setPaymentplanCollection(listPaymentPlanTemp);
					listAccountReceivableTemp.add(accountreceivable);
				}
				client.setAccountreceivableCollection(listAccountReceivableTemp);
				*/
		        List<Accountreceivable> listAccountReceivable = accountReceivableDao.getListAccountReceivableByClientIdByPharmacyIdByUserId(client.getClientId(),userPharmacy.getUserpharmacyPK().getPharmacyId(),userPharmacy.getUserpharmacyPK().getUserId());
		        for (Accountreceivable accountreceivable : listAccountReceivable) {
		        	accountreceivable.setCredititemCollection(creditItemDao.getListCreditItemByAccountReceivableId(accountreceivable.getAccountReceivableId()));
					accountreceivable.setPaymentplanCollection(paymentPlanDao.getListPaymentPlanByAccountReceivableId(accountreceivable.getAccountReceivableId()));
				}
				client.setAccountreceivableCollection(listAccountReceivable);
			}
		}
        return client;
	}	
	@Transactional(rollbackFor = {Exception.class})
	public boolean addTransaction(HttpServletRequest request,Integer s_pharmacyId,List<Product> productList,short userId){
		/*
		Enumeration enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String parameterName = (String) enumeration.nextElement();
            System.out.print(parameterName+"   < ***** >");
            System.out.println(request.getParameter(parameterName));
        }*/		
		boolean temp = false;
		temp = true;		

		List<Item> listItempTempToCreditItem = new ArrayList<Item>();

		String clientType = "TypeClientValue";
		Operation operation = operationDao.findByOperationId((short)1);
		
		int indexMax = UtilConverter.converterStringToInteger(request,"indexRowDetailItems");
		for (int i = 0; i < indexMax; i++) {
			String tempString = UtilConverter.converterStringToString(request,"dp_"+i);
			if(tempString != null){
				String array[] = tempString.split("\\|\\|");
				String arrayValues[] = array[0].split("_");
				String arrayPackage[]= array[1].split("\\*\\*");	
				for (int j = 0; j < arrayPackage.length; j++) {
					Transaction t = new Transaction(UtilCommon.getPeriod(),transactionDao.findTransactionIdByPeriodIdsMax(UtilCommon.getPeriod())+1);
					short unit = Short.parseShort(arrayValues[5]);
					//short stockId = Short.parseShort(arrayValues[4]);
					float amount = Float.parseFloat(arrayValues[6]);
					Package p = packageDao.findPackageByPackageId(Integer.parseInt(arrayPackage[j]));
					t.setUnits(unit);
					t.setAmount(amount);
					t.setPackageId(p);
					t.setOperationId(operation);
					t.setDateTransaction(new Date());
					t.setUserpharmacy(userPharmacyDao.findByPharmacyIdByUserId(UtilConverter.converterStringToInteger(request,"pharmacyId"),UtilConverter.converterStringToInteger(request,"userId")));
											
					String nitCI = UtilConverter.converterStringToString(request,"_nitCi");
					List<Client> listClient = clientDao.getListClientByPharmacyIdByNitByActive(s_pharmacyId,nitCI,true);
					for (Client client : listClient) {
						if(client.getNit().equals(nitCI))
							t.setClientId(client);
					}
					if(listClient.isEmpty()){
						Pharmacy pharmacy = pharmacyDao.findByPharmacyId(s_pharmacyId);
						Client client = new Client(null, clientType,UtilConverter.converterStringToString(request,"_client"),true);
						client.setNit(nitCI);
						client.setPharmacyId(pharmacy);
						clientDao.add(client);
						t.setClientId(client);
					}
					
					Dosage d = dosageDao.getListDosageByOrderBy((short)1).get(0);//operationNationalTaxationService.getListDosageByPharmacyIdOrderBy(s_pharmacyId).get(0);
					
					String codigoDeControl = billDao.getCodigoDeControl(Long.parseLong(""+d.getOrderNumber()),(long)d.getLastNumBillIssued(),Long.parseLong(nitCI) ,Long.parseLong(UtilCommon.getFecha_yyyyMMdd()+""), amount,d.getKey());
					
					Bill bill = new Bill(UtilCommon.getPeriod(),billDao.findBillIdIdMax());
					bill.setClient(true);
					bill.setDate(new Date());
					bill.setNit(nitCI);
					bill.setBillNum(d.getLastNumBillIssued());
					bill.setAmount(amount);
					bill.setControlCode(codigoDeControl);
					bill.setDosageId(d);
					billDao.add(bill);
					t.setBill(bill);
					transactionDao.add(t);
					temp = true;
				}
			}
		}			
		for (Product p : productList) {
			for (Package package1 : p.getPharmacyProduct().getPackageCollection()) {
				Package pack = packageDao.findPackageByPackageId(package1.getPackageId());
				if(pack.getState() == (short)3){
					Package pac = packageDao.findPackageByPackageId(pack.getPackageId());
					pac.setState((short)2);
					packageDao.update(pac);
				}
				for (Item item : package1.getItemCollection()) {
					if(item.getState() == (short)3 && userId == item.getTempState()){
						Item i = itemDao.findItemByPackageIdByItemId(item.getItemPK().getPackageId(),item.getItemPK().getItemId());
						i.setState((short)2);
						listItempTempToCreditItem.add(i);
						itemDao.update(i);
					}
				}
			}
		}	
				
		
		// start to paymentplan
		int searchTypePayment = UtilConverter.converterStringToInteger(request,"searchTypePayment");
		Userpharmacy up = userPharmacyDao.findByPharmacyIdByUserId(s_pharmacyId,(int)userId);
		Client client = clientDao.getListClientByNitByActive(UtilConverter.converterStringToString(request,"_nitCi"),true).get(0);
		Accountreceivable accountReceivable = new Accountreceivable(null,new Date());
		
		String obs = UtilConverter.converterStringToString(request,"_observations");
		if(obs != null)
			accountReceivable.setRemark(obs);
		
		if(searchTypePayment == 1){
			float amountCredit = UtilConverter.converterStringToFloat(request,"amount1");
			if(amountCredit != -1.0f)
				accountReceivable.setAmount(amountCredit);
		}			
		if(searchTypePayment == 3){	
			float amountCredit = UtilConverter.converterStringToFloat(request,"totalAmountCredit");
			if(amountCredit != -1.0f)
				accountReceivable.setAmount(amountCredit);
		}
		accountReceivable.setUserpharmacy(up);
		accountReceivable.setActive(true);
		accountReceivable.setClientId(client);
		accountReceivableDao.add(accountReceivable);			
		
		for (Product p : productList) {
			for (Package package1 : p.getPharmacyProduct().getPackageCollection()) {
				Package pack = packageDao.findPackageByPackageId(package1.getPackageId());
				for (Item item : package1.getItemCollection()) {
					if(userId == item.getTempState()){
						for (Item itemTemp : listItempTempToCreditItem) {
							if(itemTemp.getItemPK().getItemId() == item.getItemPK().getItemId() && itemTemp.getItemPK().getPackageId() == item.getItemPK().getPackageId()){
								Item i = itemDao.findItemByPackageIdByItemId(item.getItemPK().getPackageId(),item.getItemPK().getItemId());							
								Credititem crediItem = new Credititem(accountReceivable, i);
								creditItemDao.add(crediItem);
							}
						}
					}
				}
			}
		}
				
		if(searchTypePayment == 1)
			addPaymentPlan1(request,accountReceivable);
		if(searchTypePayment == 3)					
			addPaymentPlan3(request, accountReceivable, UtilConverter.converterStringToInteger(request,"amountPayment"));	
		// end to paymentplan
		return temp;
	}	
	public void addPaymentPlan3(HttpServletRequest request,Accountreceivable accountReceivable,int countPayment){
    	try {
    		short j=1;
    		Date date=new Date();
    		short frecuencyPayment = Short.parseShort(request.getParameter("frequencyPayment"));
    		for (int i = 0; i < countPayment; i++) {
    			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                date= df.parse(request.getParameter("expireDateInput3_"+i));                
                Paymentplan pPlan = new Paymentplan(null,true,Float.parseFloat(request.getParameter("amount3_"+i)),true,false);
                pPlan.setExpirationDate(date);
                pPlan.setAccountReceivableId(accountReceivable);
                pPlan.setDetail(request.getParameter("detail3_"+i));
                paymentPlanDao.add(pPlan);
                //paymentPlanEnrollmentDao.add(new Paymentplanenrollment(j,enrollment, paymentType,Float.parseFloat(request.getParameter("amount3_"+i)),date, active,valueStringDetail, paid,frecuencyPayment));
                j++;
			}    		
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
	public void addPaymentPlan1(HttpServletRequest request,Accountreceivable accountReceivable){
		try {
			Date date=new Date();
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            date= df.parse(request.getParameter("expirationDate1"));                
            Paymentplan pPlan = new Paymentplan(null,true,Float.parseFloat(request.getParameter("amount1")),true,false);
            pPlan.setExpirationDate(date);
            pPlan.setAccountReceivableId(accountReceivable);
            pPlan.setDetail(request.getParameter("detail1"));
            paymentPlanDao.add(pPlan);
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
	
	
	
	@Transactional(readOnly = true)
	public List<Accountreceivable> getListAccountReceivable(){
		return accountReceivableDao.getListAccountReceivable();
	}
	@Transactional(readOnly = true)
	public List<Accountreceivable> getListAccountReceivableByClientId(Integer clientId){
		return accountReceivableDao.getListAccountReceivableByClientId(clientId);
	}
	@Transactional(readOnly = true)
	public List<Accountreceivable> getListAccountReceivableByPharmacyIdByUserId(Integer pharmacyId,Integer userId){
		return accountReceivableDao.getListAccountReceivableByPharmacyIdByUserId(pharmacyId, userId);
	}
	@Transactional
	public void removeAccountReceivable(Accountreceivable accountReceivable){
		accountReceivableDao.remove(accountReceivable);
	}    
	@Transactional
    public void updateAccountReceivable(Accountreceivable accountReceivable){
    	accountReceivableDao.update(accountReceivable);
    }
    @Transactional
    public void addAccountReceivable(Accountreceivable accountReceivable){
    	accountReceivableDao.add(accountReceivable);
    }
    @Transactional(readOnly = true)
    public Accountreceivable searchAccountReceivableByAccountReceivableId(Integer accountReceivableId){
    	return accountReceivableDao.findAccountReceivableByAccountReceivableId(accountReceivableId);
    }
    
    
    @Transactional(readOnly = true)
    public List<Paymentplan> getListPaymentPlan(){
    	return paymentPlanDao.getListPaymentPlan();
    }
    @Transactional(readOnly = true)
	public List<Paymentplan> getListPaymentPlanByAccountsPayableId(Integer accountsPayableId){
		return paymentPlanDao.getListPaymentPlanByAccountsPayableId(accountsPayableId);
	}
	@Transactional(readOnly = true)
	public List<Paymentplan> getListPaymentPlanByAccountReceivableId(Integer accountReceivableId){
		return paymentPlanDao.getListPaymentPlanByAccountReceivableId(accountReceivableId);
	}
	@Transactional
	public void removePaymentPlan(Paymentplan paymentPlan){
		paymentPlanDao.remove(paymentPlan);
	}    
	@Transactional
    public void updatePaymentPlan(Paymentplan paymentPlan){
    	paymentPlanDao.update(paymentPlan);
    }
    @Transactional
    public void addPaymentPlan(Paymentplan paymentPlan){
    	paymentPlanDao.add(paymentPlan);
    }
    @Transactional(readOnly = true)
    public Paymentplan searchPaymentPlanByPaymentPlanId(short paymentPlanId){
    	return paymentPlanDao.findPaymentPlanByPaymentPlanId(paymentPlanId);
    }
    
    
    @Transactional(readOnly = true)
    public List<Accountspayable> getListAccountsPayable(){
    	return accountsPayableDao.getListAccountsPayable();
    }
    @Transactional(readOnly = true)
	public List<Accountspayable> getListAccountsPayableByProviderId(Integer providerId){
		return accountsPayableDao.getListAccountsPayableByProviderId(providerId);
	}
	@Transactional
	public void removeAccountsPayable(Accountspayable accountsPayable){
		accountsPayableDao.remove(accountsPayable);
	}    
	@Transactional
    public void updateAccountsPayable(Accountspayable accountsPayable){
    	accountsPayableDao.update(accountsPayable);
    }
    @Transactional
    public void addAccountsPayable(Accountspayable accountsPayable){
    	accountsPayableDao.add(accountsPayable);
    }
    @Transactional(readOnly = true)
    public Accountspayable searchAccountsPayableByAccountsPayableId(Integer accountsPayableId){
    	return accountsPayableDao.findAccountsPayableByAccountsPayableId(accountsPayableId);
    }
}
